#-*-coding:utf8;-*-
#qpy:pygame

import sys, pygame 
from pygame.locals import *
from random import randrange
import androidhelper
droid=androidhelper.Android()
up =lambda x:(x[0]-1,x[1])
down = lambda x :(x[0]+1,x[1])
left = lambda x : (x[0],x[1]-1)
right = lambda x : (x[0],x[1]+1) 
tl = lambda x :x<3 and x+1 or 0 
tr = lambda x :x==0 and 3 or x-1 
dire = [up,left,down,right] 
move = lambda x,y:[y(x[0])]+x[:-1] 
grow = lambda x,y:[y(x[0])]+x 
s = [(15,7),(15,6),(15,5)]
s2 = [(30,5),(30,6),(30,7)]
d = down
d2 = up
FPSCLOCK=pygame.time.Clock() 
pygame.init()
infoObject = pygame.display.Info()
food = randrange(0,infoObject.current_h/40),randrange(0,infoObject.current_w/40) 
pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
pygame.mouse.set_visible(1) 
screen = pygame.display.get_surface() 
screen.fill((0,0,0)) 
times=0.0
foodNumber = 49

def changeDriection(d,s,pos):
    d1 = d
    if d==up:
        if pos[0]-s[1]*40>0:
            d1=dire[tr(dire.index(d))]
        elif pos[0]-s[1]*40<0:
            d1=dire[tl(dire.index(d))]
    elif d==down:
        if pos[0]-s[1]*40<0:
            d1=dire[tr(dire.index(d))]
        elif pos[0]-s[1]*40>0:
            d1=dire[tl(dire.index(d))]
    elif d==right:
        if pos[1]-s[0]*40>0:
            d1=dire[tr(dire.index(d))]
        elif pos[1]-s[0]*40<0:
            d1=dire[tl(dire.index(d))]
    elif d==left:
        if pos[1]-s[0]*40<0:
            d1=dire[tr(dire.index(d))]
        elif pos[1]-s[0]*40>0:
            d1=dire[tl(dire.index(d))]
    return d1

while True:
    
    time_passed = FPSCLOCK.tick(30) 
    if times>=150: 
        times =0.0 
        s = move(s,d)
        s2 = move(s2,d2)
    else: 
        times +=time_passed
    for event in pygame.event.get():
        if event.type == QUIT: 
            sys.exit() 
        elif event.type == pygame.MOUSEBUTTONDOWN:
            d = changeDriection(d,s[0],event.pos)
    d2 = changeDriection(d2,s2[0],(food[1]*40,food[0]*40))        
    if s[0]==food:
        s = grow(s,d) 
        food =randrange(0,(infoObject.current_h/40)),randrange(0,(infoObject.current_w/40))
        foodNumber -= 1

    if s2[0]==food: 
        s2 = grow(s2,d2) 
        food =randrange(0,(infoObject.current_h/40)),randrange(0,(infoObject.current_w/40))
        foodNumber -= 1
    
    screen.fill((0,0,0))
    for r,c in s: 
        pygame.draw.rect(screen,(255,0,0),(c*40,r*40,40,40))
    for r,c in s2: 
        pygame.draw.rect(screen,(0,0,255),(c*40,r*40,40,40))
    if foodNumber>0:
        pygame.draw.circle(screen,(0,255,0),(food[1]*40+20,food[0]*40+20),20)
    pygame.display.update()
    if s[0] in s[1:] or s[0][0]<0 or s[0][0] >= (infoObject.current_h/40) or s[0][1]<0 or s[0][1]>=(infoObject.current_w/40):
        if s[0] in s[1:]:
            droid.makeToast('You Eat Yourself Game Over!')
        else:
            droid.makeToast('Game Over!')
        break
    elif s2[0] in s2[1:] or s2[0][0]<0 or s2[0][0] >= (infoObject.current_h/40) or s2[0][1]<0 or s2[0][1]>=(infoObject.current_w/40):
        if s2[0] in s2[1:]:
            droid.makeToast('It Eat Itself You Win!')
        else:
            droid.makeToast('You Win!')
        break
    elif foodNumber == 0:
        if len(s)>len(s2):
            droid.makeToast('Food Finshed You Win!')
            break
        else:
            droid.makeToast('Food Finshed Game Over!')
            break
        
    
